/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.services.impl;

import cz.muni.fi.pa165.creatures.dao.web.PersonFunctionDAO;
import cz.muni.fi.pa165.creatures.dto.web.FunctionDTO;
import cz.muni.fi.pa165.creatures.dto.web.PersonDTO;
import cz.muni.fi.pa165.creatures.dto.web.PersonFunctionDTO;
import cz.muni.fi.pa165.creatures.dto.web.mapping.FunctionMapping;
import cz.muni.fi.pa165.creatures.dto.web.mapping.PersonFunctionMapping;
import cz.muni.fi.pa165.creatures.dto.web.mapping.PersonMapping;
import cz.muni.fi.pa165.creatures.model.web.Function;
import cz.muni.fi.pa165.creatures.services.PersonFunctionService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents actual implementation of the
 * {@link cz.muni.fi.pa165.creatures.services.PersonFunctionService}.
 *
 * @author xmiklos1
 */
@Service
public class PersonFunctionServiceImpl implements PersonFunctionService {

    private static final Logger logger =
            Logger.getLogger(PersonFunctionServiceImpl.class.getName());

    @Autowired
    PersonFunctionDAO personFunctionDAO;

    @Autowired
    PersonMapping personMapping;

    @Autowired
    FunctionMapping functionMapping;
    
    @Autowired
    PersonFunctionMapping pfMapping;

    public PersonFunctionDAO getPersonFunctionDAO() {
        return personFunctionDAO;
    }

    public void setPersonFunctionDAO(PersonFunctionDAO personFunctionDAO) {
        this.personFunctionDAO = personFunctionDAO;
    }

    public PersonMapping getPersonMapping() {
        return personMapping;
    }

    public void setPersonMapping(PersonMapping personMapping) {
        this.personMapping = personMapping;
    }

    public FunctionMapping getFunctionMapping() {
        return functionMapping;
    }

    public void setFunctionMapping(FunctionMapping functionMapping) {
        this.functionMapping = functionMapping;
    }

    @Override
    @Transactional
    public void create(PersonFunctionDTO personFunction) {
        personFunctionDAO.create(pfMapping.DTOtoEntity(personFunction));
    }

    @Override
    @Transactional
    public void remove(PersonFunctionDTO personFunction) {
        personFunctionDAO.remove(pfMapping.DTOtoEntity(personFunction));
    }

    @Override
    @Transactional
    public boolean hasFunction(PersonDTO person, FunctionDTO function) {
        try {
            return personFunctionDAO.hasFunction(personMapping.DTOtoEntity(person),
                    functionMapping.DTOtoEntity(function));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public List<FunctionDTO> getFunctions(String name) {
        System.err.println("getFunctions dostal name = " + name);
        List<FunctionDTO> functionDTOs = new ArrayList<FunctionDTO>();

        if (personFunctionDAO == null) {
            System.err.println("THIS IS FUCKING NUUUUUUL");
        }

        List<Function> functions = personFunctionDAO.getFunctions(name);

        for (Function f : functions) {
            functionDTOs.add(functionMapping.entityToDTO(f));
        }
        return functionDTOs;
    }
}